package org.micode.demo.db.service.impl;

import org.micode.common.helper.I18nMsgHelper;
import org.micode.demo.db.dao.SysRoleDao;
import org.micode.demo.db.dao.SysRoleMenuDao;
import org.micode.demo.db.dao.SysUserRoleDao;
import org.micode.demo.db.domain.dto.SysRoleDTO;
import org.micode.demo.db.domain.dto.SysRoleMenuDTO;
import org.micode.demo.db.domain.entity.SysRoleDO;
import org.micode.demo.db.domain.query.SysRoleQuery;
import org.micode.demo.db.service.DaoSysRoleService;
import org.micode.common.domain.vo.EditObjVO;
import org.micode.common.utils.StringExtUtils;
import org.micode.libs.dao.base.BaseAllServiceImpl;
import org.micode.libs.dao.helper.IdHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色信息(sys_role) 服务实现类
 *
 * author: wutianbin
 */
@Service
@Slf4j
public class DaoSysRoleServiceImpl
        extends BaseAllServiceImpl<Long, SysRoleDTO, SysRoleDO, SysRoleQuery, SysRoleDao>
        implements DaoSysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    protected SysRoleDao getMyDao() {
        return sysRoleDao;
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public SysRoleDTO insert(SysRoleDTO sysRole) {
        // 先添加
        SysRoleDTO ret = super.insert(sysRole);

        // 然后授权
        if (null != ret && null != ret.getRoleId()) {
            sysRole.setRoleId(ret.getRoleId());
            saveRoleResources(sysRole);
        }

        return ret;
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int deleteByKeys(EditObjVO<List<Long>> delIds) {
        List<Long> filterIds = listNotDefault(delIds.getWantEdit());
        if (null == filterIds || filterIds.size() <= 0) {
            return 0;
        }

        String ids = getKeysString(filterIds);
        if (!StringExtUtils.isAnyEmpty(ids)) {
            // 删除这些keys对应的权限
            deleteRolePerms(filterIds);

            return sysRoleDao.updateDeleteForIds(ids, delIds.getUpdUser(),
                    null == delIds.getUpdProg() ? "" : delIds.getUpdProg(),
                    null == delIds.getUpdIp() ? "" : delIds.getUpdIp());
        } else {
            return 0;
        }
    }

    @Override
    public List<Long> listNotDefault(List<Long> roleIds) {
        String idStr = IdHelper.getIdsStr(roleIds);
        return sysRoleDao.listRoleIdNotDefault(idStr);
    }

    private void deleteRolePerms(List<Long> roleIds) {
        try {
            if (null != roleIds && roleIds.size() > 0) {
                for (Long roleId : roleIds) {
                    sysRoleMenuDao.deleteForRoleId(roleId);
                    sysUserRoleDao.deleteForRoleId(roleId);
                }
            }
        } catch(Exception e) {
            log.error(I18nMsgHelper.get("fail.DaoSysRoleServiceImpl-deleteRolePerms.call"), e);
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int update(SysRoleDTO sysRoleDTO) {
        // 先更新
        int ret = super.update(sysRoleDTO);

        // 重置授权信息
        resetRolePerms(sysRoleDTO);

        return ret;
    }

    private void resetRolePerms(SysRoleDTO role) {
        if (null == role || null == role.getRoleId()) {
            return;
        }

        try {
            // 删除原来的授权
            sysRoleMenuDao.deleteForRoleId(role.getRoleId());
            // 添加新的授权
            saveRoleResources(role);
        } catch(Exception e) {
            log.error(I18nMsgHelper.get("fail.DaoSysRoleServiceImpl-resetRolePerms.call"), e);

        }
    }

    private void saveRoleResources(SysRoleDTO role) {
        try {
            List<Long> menus = role.getMenuIdList();
            if (null != menus && menus.size() > 0) {
                for (Long resId : menus) {
                    SysRoleMenuDTO roleRes = new SysRoleMenuDTO();
                    roleRes.setRoleId(role.getRoleId());
                    roleRes.setMenuId(resId);
                    sysRoleMenuDao.insert(roleRes);
                }
            }
        } catch(Exception e) {
            log.error(I18nMsgHelper.get("fail.DaoSysRoleServiceImpl-saveRoleResources.call"), e);
        }
    }

}
